package com.example.front.mapper;

import com.example.common.dto.Pagination;
import com.example.front.dto.LineUserDto;
import com.example.front.dto.LineUserSearchDto;
import com.example.front.entity.Line;
import com.example.front.entity.LineUser;
import com.example.front.entity.Workshop;
import com.example.passport.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface LineUserMapper {
    @Insert("INSERT INTO `line_user`" +
            " (`tenant_id`,`line_id`, `user_id`, `event_id`) VALUES" +
            " (#{tenantId}, #{lineId}, #{userId}, #{eventId})")
    void create(LineUserDto lineUserDto);

    @Select("<script>" +
            "SELECT * FROM `line_user` " +
            "<where>" +
            " `tenant_id`=#{lineUser.tenantId} " +
            "<if test='lineUser.eventId!=null'> AND `event_id` LIKE CONCAT('%',#{lineUser.eventId},'%') </if>" +
            "<if test='lineUser.lineId!=null'> AND `line_id`=#{lineUser.lineId}</if>" +
            "<if test='lineUser.userId!=null'> AND `user_id`=#{lineUser.userId}</if>" +
            "</where>" +
            "ORDER BY `id` DESC LIMIT #{pagination.offset}, #{pagination.limit}" +
            "</script>")
    List<LineUser> findAll(@Param("lineUser") LineUser lineUser, @Param("pagination") Pagination pagination);

    @Select("<script>" +
            "SELECT * FROM `line_user` " +
            "<where>" +
            " `tenant_id`=#{tenantId} " +
            "<if test='eventId!=null'> AND `event_id`=#{eventId}</if>" +
            "<if test='lineId!=null'> AND `line_id`=#{lineId} </if>" +
            "</where> LIMIT 1" +
            "</script>")
    LineUser findUserId(@Param("tenantId") Long tenantId,@Param("lineId") Long lineId,@Param("eventId")Long eventId);

    @Select("SELECT * FROM `line` WHERE `tenant_id`=#{line.tenantId} AND `name` = #{line.name} LIMIT 1")
    List<Line> findLineByName(@Param("line") Line line);

    @Select("SELECT * FROM `line` WHERE `tenant_id`=#{tenantId} AND `id` = #{id} LIMIT 1")
    Line findLineById(@Param("tenantId") Long tenantId, @Param("id") Long id);

    @Select("SELECT * FROM `line_user` WHERE `tenant_id`=#{lineUser.tenantId} AND `event_id` = #{lineUser.eventId} AND `line_id` = #{lineUser.lineId}  LIMIT 1")
    LineUser findByLineEventId(@Param("lineUser") LineUserDto lineUserDto);



    @Delete("DELETE FROM `line_user` WHERE `id` = #{id} AND `tenant_id` = #{tenantId}")
    int delete(LineUser LineUser);

    @Update("UPDATE `line_user` SET `user_id` = #{userId} " +
            " WHERE `tenant_id` = #{tenantId} AND `id` = #{id}")
    int update(LineUserDto LineUserDto);

//    @Select("<script>" +
//            "SELECT * FROM user WHERE `id` IN " +
//            "<foreach item='item' index='index' collection='ids' open='(' separator=',' close=')'>" +
//            "#{item}" +
//            "</foreach> " +
//            "</script>")
//    List<User> findUserByIds(@Param("ids") List<Long> ids);

    @Select("<script>" +
            "SELECT * FROM workshop WHERE `id` IN " +
            "<foreach item='item' index='index' collection='ids' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach> " +
            "</script>")
    List<Workshop> findWorkshopByIds(@Param("ids") List<Long> ids);

    @Select("<script>" +
            "SELECT * FROM `line` WHERE `id` IN " +
            "<foreach item='item' index='index' collection='lineIds' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach> " +
            "</script>")
    List<Line> findLineByIds(@Param("lineIds") List<Long> lineIds);

    @Select("<script>" +
            "SELECT COUNT(*) FROM `line`" +
            "<where>" +
            "line.tenant_id = #{lineUserSearchDto.tenantId} " +
            "<if test='lineUserSearchDto.eventId!=null'> AND `event_id` LIKE CONCAT('%',#{lineUserSearchDto.eventId},'%') </if>" +
            "<if test='lineUserSearchDto.lineId!=null'> AND `line_id`=#{lineUserSearchDto.lineId}</if>" +
            "<if test='lineUserSearchDto.userId!=null'> AND `user_id`=#{lineUserSearchDto.userId}</if>" +
            "</where>" +
            "</script>")
    long count(@Param("lineUserSearchDto") LineUserSearchDto lineUserSearchDto);
}
